package com.demo2do.fms.admin.web.controller.business;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.collections.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.demo2do.core.entity.page.Page;
import com.demo2do.core.entity.result.HttpResult;
import com.demo2do.core.web.interceptor.MenuSetting;
import com.demo2do.fms.admin.entity.search.business.SapSearch;
import com.demo2do.fms.admin.entity.security.Login;
import com.demo2do.fms.admin.service.business.BalanceService;
import com.demo2do.fms.admin.web.resolver.Secure;

/**
 * 勾选订单送财务
 * 
 * @author Ysy
 *
 */
@Controller
@RequestMapping(value = "/balances")
@MenuSetting("menu-business")
public class BalanceController {
    
    private static final Logger logger = LoggerFactory.getLogger(BalanceController.class);
    
    @Autowired
    private BalanceService balanceService;
    
    /**
     * 勾选订单送SAP - search panel
     * 
     * @param login
     * @param page
     * @return
     */
    @RequestMapping(value = "/sap-search", method = RequestMethod.GET)
    @MenuSetting("submenu-balance-sap")
    public ModelAndView sapSearch(@Secure Login login, Page page){

        ModelAndView modelAndView = new ModelAndView("business/balance/sap-search");
        
        modelAndView.addObject("region", login.isRegionUser());
        modelAndView.addObject("orgIds", login.getCurrOrgIds());
        
        return modelAndView;
        
    }
    
    /**
     * 勾选订单送SAP - on search
     * 
     * @param search
     * @param request
     * @return
     */
    @RequestMapping(value = "", method = RequestMethod.POST, params= "sap")
    @MenuSetting("submenu-balance-sap")
    public ModelAndView onSapSearch(SapSearch search, HttpServletRequest request) {
        
        ModelAndView modelAndView = new ModelAndView("business/balance/sap-list");
        
        List<Map<String, Object>> saps = balanceService.listSap(search);
        modelAndView.addObject("saps", saps);
        
        modelAndView.addObject("collectStatus", search.getCollectStatus());
        modelAndView.addObject("queryString", request.getQueryString());
        
        return modelAndView;
        
    }
    
    /**
     * 提交勾选订单送财务
     * 
     * @param shipmentFinanceIds
     * @return
     */
    @RequestMapping(value = "/sap-send", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public @ResponseBody HttpResult deliverOrder(@RequestParam("shipmentFinanceId") List<Long> shipmentFinanceIds) {
    
        HttpResult httpResult = new HttpResult();
        
        try {
            
            logger.warn("#deliverOrder() - deliver order to sap, size=[{}]", shipmentFinanceIds.size());
            
            // 1. 对勾选Finance进行分组送财务
            List<Long> subShipmentFinanceIds = new ArrayList<Long>();
            for (int i = 0; i < shipmentFinanceIds.size(); i++) {
                
                subShipmentFinanceIds.add(shipmentFinanceIds.get(i));
                
                if (subShipmentFinanceIds.size() % 20 == 0) {
                    balanceService.deliverOrder(subShipmentFinanceIds);
                    subShipmentFinanceIds = new ArrayList<Long>();
                }
                
            }
            
            // 2. 将最后一组Finance送财务
            if (CollectionUtils.isNotEmpty(subShipmentFinanceIds)) {
                balanceService.deliverOrder(subShipmentFinanceIds);
            }
            
            httpResult.success().message("勾选订单送财务成功");
            
        } catch (Exception e) {
            httpResult.fail("500").message(e.getMessage());
            logger.error("#deliverOrder() - deliver order to sap fail, exception is [{}]", e);
        }
        
        return httpResult;
        
    }

}
